﻿using AutoMapper;
using B.S.BasicData.Domain.SystemUsers;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interfaces;
using B.S.BasicData.Write.Api.Application.Command.SystemUsers.IAMCommand;
using MediatR;

namespace B.S.BasicData.Write.Api.Application.CommandHandler.SystemUsers.IAMCommand
{
    /// <summary>
    /// 编辑身份验证管理
    /// </summary>
    public class IAMUpdateCommandHandler : IRequestHandler<IAMUpdateCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<IAM_Model> _baseRepository;
        private readonly IMapper mapper;

        public IAMUpdateCommandHandler(IBaseRepository<IAM_Model> baseRepository, IMapper mapper)
        {
            _baseRepository = baseRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 编辑身份验证管理
        /// </summary>
        public Task<ApiResult<int>> Handle(IAMUpdateCommand request, CancellationToken cancellationToken)
        {
            var res = new ApiResult<int>();
            var entity = mapper.Map<IAM_Model>(request);
            var result = _baseRepository.Update(entity);
            if (result > 0) 
            {
                res.Msg = "修改成功";
                res.Code = ApiEnums.Success;
                res.Data = result;
            }
            return Task.FromResult(res);
        }
    }
}
